package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.ITravelCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    // 高级查询
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * spring-data 会自动注入ITravelCommentRepository实现类
     *
     * ITravelCommentRepository 接口继承MongoRepository
     * 拥有了 分页,排序,crud基本功能
     *    底层实现 : AOP 操作--动态代理
     */
    @Autowired
    private ITravelCommentRepository travelCommentRepository;

    @Override
    public TravelComment saveOrUpdate(TravelComment comment) {
        if(!StringUtils.hasLength(comment.getId())){
            comment.setCreateTime(new Date());
        }
        if(comment.getType() == TravelComment.TRAVLE_COMMENT_TYPE){ //评论的评论
            if(StringUtils.hasLength(comment.getRefComment().getId())){
                comment.setRefComment(travelCommentRepository.findById(comment.getRefComment().getId()).get());// 封装关联的评论
            }
        }
        System.out.println(comment);
        travelCommentRepository.save(comment);
        return comment;
    }

    @Override
    public Page query(TravelCommentQuery qo) {
        Query query = new Query(); // 加 core 包
        query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        long count = mongoTemplate.count(query, TravelComment.class);
        System.out.println("count: "+count);
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "travelId");
        query.with(pageable);
        List<TravelComment> list = mongoTemplate.find(query, TravelComment.class);
        System.out.println("list: "+list);
        return new PageImpl(list, pageable, count);
    }

    @Override
    public Long getCount(Long travelId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("travelId").is(travelId));
        long count = mongoTemplate.count(query, TravelComment.class);
        return count;
    }

    @Override
    public List<TravelComment> OrderByCreateTimeTop10() {
        Query query = new Query();
        PageRequest pageRequest = PageRequest.of(0, 10, Sort.Direction.DESC, "createTime");
        query.with(pageRequest);
        return mongoTemplate.find(query, TravelComment.class);
    }
}
